package com.lelv.business.travelInfo.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;

import com.lelv.business.travelInfo.bo.ITravelArticelBo;
import com.lelv.business.travelInfo.model.TravelArticle;
import com.lelv.framework.action.BaseAction;
import com.lelv.framework.plugin.LelvUtils;
import com.lelv.framework.plugin.Page;

public class TravelArticleAction extends BaseAction{

	private ITravelArticelBo travelArticleBo;
	private Page page = new Page();
	private List<TravelArticle> articleList = new ArrayList<TravelArticle>();
	private TravelArticle travelArticle;
	private File myFile;
	private String myFileContentType;
	private String myFileFileName;
	private Integer state = 0;

	public String findTravelArticle(){

		this.travelArticle = this.travelArticleBo.findTravelArticleById(travelArticle.getId());

		return SUCCESS;
	}

	public String getTravelArticleList(){

		this.page = this.travelArticleBo.getTravelArticleList(page,getCurrentUserId(),(Integer)getSession().getAttribute("adminLevel"));
		this.articleList = page.getList();

		return SUCCESS;

	}

	//添加文章
	public String addTravelArticle() throws Exception{

		//基于myFile创建一个文件输入流
        InputStream is = new FileInputStream(myFile);
        // 设置上传文件目录
        String uploadPath = ServletActionContext.getServletContext().getRealPath("/framework/advImgs");

        if(state != 0){
        	this.myFileFileName = LelvUtils.createSystemFileName(myFileFileName);
        }

        // 设置目标文件
        File toFile = new File(uploadPath, this.getMyFileFileName());

        // 创建一个输出流
        OutputStream os = new FileOutputStream(toFile);

        //设置缓存
        byte[] buffer = new byte[1024];

        int length = 0;

        //读取myFile文件输出到toFile文件中
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }

        //关闭输入流
        is.close();

        //关闭输出流
        os.close();

        travelArticle.setArticlePic(myFileFileName);
		travelArticle.setCreatorId(getCurrentUserId());
		travelArticle.setCreateTime(new Date());
		this.travelArticleBo.addTravelArticle(travelArticle);

		return "addOK";
	}

	public String updateTravelArticleForward(){

		this.travelArticle = this.travelArticleBo.findTravelArticleById(travelArticle.getId());

		return SUCCESS;
	}

	//修改文章
	public String updateTravelArticle(){

		TravelArticle TempTravelArticle = this.travelArticleBo.findTravelArticleById(travelArticle.getId());
		BeanUtils.copyProperties(travelArticle, TempTravelArticle, new String[]{"id","creatorId","createTime"});
		this.travelArticleBo.updateTravelArticle(TempTravelArticle);

		return "updateOK";
	}

	//删除文章
	public String deleteTravelArticle(){

		this.travelArticleBo.deleteTravelArticle(travelArticle.getId());

		return "deleteOK";
	}

	public ITravelArticelBo getTravelArticleBo() {
		return travelArticleBo;
	}

	public void setTravelArticleBo(ITravelArticelBo travelArticleBo) {
		this.travelArticleBo = travelArticleBo;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<TravelArticle> getArticleList() {
		return articleList;
	}

	public void setArticleList(List<TravelArticle> articleList) {
		this.articleList = articleList;
	}

	public TravelArticle getTravelArticle() {
		return travelArticle;
	}

	public void setTravelArticle(TravelArticle travelArticle) {
		this.travelArticle = travelArticle;
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFileContentType() {
		return myFileContentType;
	}

	public void setMyFileContentType(String myFileContentType) {
		this.myFileContentType = myFileContentType;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

}
